﻿/**
 * 
 * Copyright 2009, 2010 Mike Jamer [email: mikejamer@gmail.com ]
 * 
 * This File is part of Moose IRC
 * 
 * MooseIRC is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * MooseIRC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with MooseIRC.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace moose.irc
{
    /// <summary>
    /// IRC Channel
    /// </summary>
    [CLSCompliant(true)]
    public class IrcChannel : IrcEntity
    {

        #region [Local Attributes]

        /// <summary>
        /// Reference to the IRC Session
        /// </summary>
        private IrcSession m_session; //TODO: determine if this is really needed..., maybe not...
        
        /// <summary>
        /// Channel Topic
        /// </summary>
        private string m_topic;

        /// <summary>
        /// Channel Modes
        /// </summary>
        private List<IrcChannelMode> m_channel_modes;

        /// <summary>
        /// Users in channel
        /// </summary>
        private IrcUsersList m_users;

        #endregion

        #region [Public Properties]

        /// <summary>
        /// Gets/Sets Channel Topic
        /// </summary>
        public string Topic
        {
            get
            {
                return m_topic;
            }
            set
            {
                m_topic = value;
                return;
            }
        }

        #endregion

        #region [Construction]

        /// <summary>
        /// Initialize IRC Channel
        /// </summary>
        /// <param name="name">Name of channel</param>
        public IrcChannel(IrcSession session, string name)
            : base(name)
        {
        	m_session = session;
            m_channel_modes = new List<IrcChannelMode>();
            m_users = new IrcUsersList();

            return;
        }

        #endregion

        #region [Channel Modes]

        /// <summary>
        /// Parses Mode String, and updates channel modes accordingly
        /// </summary>
        /// <param name="mode_string">mode string to parse, and use to update channel modes</param>
        /// <remarks>
        /// This is pretty ugly, however, it does what I want it to...
        /// </remarks>
        public void update_modes(string mode_string)
        {

            IrcChannelMode mode = null;
            bool add = false;
            char c = (char)0x0;
            mode_string = mode_string.Trim();
            for (int i = 0; i < mode_string.Length; i++)
            {
                c = mode_string[i];
                if (c == '+')
                {
                    add = true;
                    continue;
                }
                else if (c == '-')
                {
                    add = false;
                    continue;
                }
                if (add)
                {
                    var channel_modes = from cm in m_channel_modes
                                        where cm.Mode == c
                                        select cm;
                    if (channel_modes.Count() > 0)
                    {
                        mode = channel_modes.First();
                    }
                    else
                    {
                        mode = new IrcChannelMode(c);
                        m_channel_modes.Add(mode);
                    }

                    if (i < mode_string.Length - 1)
                    { //Test if there are any arguments for this item
                        if (mode_string[i + 1] == ' ')
                        { //There is an argument
                            string arg = mode_string.Substring(i).Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)[1];
                            if (arg == "*")
                            {
                                //clear arguments for this mode
                                mode.clear_arguments();
                            }
                            else
                            {
                                //Append this argument
                                mode.add_arg(mode_string.Substring(mode_string.IndexOf(' ')).Replace(':', ' ').Trim());
                            }
                            break;
                        }
                    }
                }
                else
                {
                    var channel_modes = from cm in m_channel_modes
                                        where cm.Mode == c
                                        select cm;
                    if (channel_modes.Count() > 0)
                    {
                        mode = channel_modes.First();
                        if (i < mode_string.Length - 1)
                        { //Test if there are any arguments for this item
                            if (mode_string[i + 1] == ' ')
                            { //There is an argument
                                string arg = mode_string.Substring(i).Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)[1];
                                if (arg == "*")
                                {
                                    //Clear, so delete the mode
                                    m_channel_modes.Remove(mode);
                                }
                                else
                                {
                                    //remove this argument
                                    mode.remove_arg(mode_string.Substring(mode_string.IndexOf(' ')).Trim());
                                    if (mode.get_arguments().Count() == 0) //Of there are no more arguments, then remove this
                                        m_channel_modes.Remove(mode);
                                }
                            }
                        }
                        else
                        {
                            //No arguments specified,
                            m_channel_modes.Remove(mode);
                        }
                    }
                }
            }
            return;
        }

        #endregion

        #region [Users]

        /// <summary>
        /// Add a user to the channel
        /// </summary>
        /// <param name="user_string">user string to parse</param>
        public void add_user(string user_string) {
            char mode = user_string[0];
            if (mode == '@' || mode == '+')
                user_string = user_string.Substring(1);

            m_users.get(user_string);

            //If we need to escalate user permissions within channel, we should do so
            if (mode == '@' || mode == '+')
                update_modes('+' + mode + ' ' + user_string);

        	return;
        }
        
        /// <summary>
        /// Remove user from channel
        /// </summary>
        /// <param name="user_string">user string to parse</param>
        public void remove_user(string user_string) {

            char mode = user_string[0];
            if (mode == '@' || mode == '+')
                user_string = user_string.Substring(1);

            m_users.remove(user_string);

            //Remove Extra permissions for this user here if we need to
            if (mode == '@' || mode == '+')
                update_modes('-' + mode +  ' ' + user_string);

        	return;
        }
        
        #endregion

    }
}
